/**
 * 客户黑名单
 * @author miaoyl
 * @date 2019/11/18
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_head_where = {},
        cols = [[
            {type: 'checkbox', fixed: 'left'},
            {field: 'GWBZ001',title:'客户编号',width:100,search:"default",searchOpts:{icon:true}},
            {field: 'GWAKL004',title:'客户名称',width:100},
            {field: 'GWBZORIG_NAME',title:'所属部门',width:100},
            {field: 'GWADL004',title:'所属区域',width:100},
            {field: 'GWBZ002',title:'黑名单等级',width:130,sort:true, filterDatas:app.datas_("crm_gwai306_gwbz002"),
                search:"default",searchOpts:{type:'select'}},
            {field: 'GWBZACTI',title:'状态',width:100,filterDatas:app.datas_("sys_y_n")},
            {
                width: 60,
                title: '',
                toolbar: '#script-gwai306setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        moduleName = "客户黑名单",
        gwai306SettingAction
    ;

    var Gwai300 = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwai306',
                _searchs: [{
                    id: 'gwai306', tableId: 'gwai306_table', datas: cols[0]
                }],
                _loadTplsFull: [
                    {
                        dom: 'gwai306_edit', tpl: 'gwai306_edit'
                    },
                    {
                        dom: 'common_open_window', tpl: 'common/_open_window'
                    }
                ]
            }, win);
        },

        initHeadTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwai306_table'),
                    url: g_ctxPath + 'crm/gwai306/getHeadList',
                    /*exportsUrl: g_ctxPath + 'crm/gwai306/getHeadList',
                     exportsName: moduleName + '数据',*/
                    importsUrl: g_ctxPath + 'crm/gwai306/importsData',
                    exportsMbUrl:g_ctxPath +'files/客户黑名单.xlsx',
                    title: moduleName + '信息表',
                    method: 'post',
                    id: 'gwai306_table',
                    size: _layuiConfig.ui_size,
                    autoSort: false, //禁用前端自动排序
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports_import'],
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: '',
                    done: function (res, curr, count) {

                    }
                });

                layui.laytpl(app.getByid(doc, 'script-gwai306setting-action').innerHTML).render({}, function (string) {
                    gwai306SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(gwai306_table)', function (obj) {

                    layer.tips(gwai306SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        self._openTplFull({
                                            tag: 'gwai306_edit',
                                            tagData: {
                                                obj:obj
                                            },
                                            index: 0,
                                            openOpts: {
                                                title: moduleName + '信息编辑',
                                                area: ['800px', '400px']
                                            }
                                        });
                                        break;
                                    case '1':
                                        self.delData(obj);
                                        break;
                                }
                                layer.close(index);
                            });
                        }
                    });
                });


                //监听行双击事件
                table.on('rowDouble(gwai306_table)', function (obj) {
                    self._openTplFull({
                        tag: 'gwai306_edit',
                        tagData: {
                            detailReadonly:true,
                            obj: obj
                        },
                        index: 0,
                        openOpts: {
                            title: moduleName + '编辑',
                            area: ['600px', '465px']
                        }
                    });
                });

                //监听事件
                table.on('toolbar(gwai306_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwai306_edit',
                                tagData: {
                                    is_add: true
                                },
                                index: 0,
                                openOpts: {
                                    title: moduleName + '编辑',
                                    area: ['800px', '400px']
                                }
                            });
                            break;
                        case 'del':
                            self.delDataBatch();
                            break;
                        case 'valid' :
                            self.valid();
                            break;
                        case 'invalid' :
                            self.invalid();
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwai306_table', where: table_head_where});
        },

        delData: function (obj) {
            app.ajax({
                xurl: 'crm/gwai306/del',
                xdata: obj.data,
                loading: true,
                confirm:{msg: "是否删除选择的数据？"},
                okmsg: moduleName + '删除成功',
                cb: function (result) {
                    if (result.ok) {
                        self.reloadTableCurr();
                    }
                }
            });

        },

        delDataBatch: function () {
            var checkStatus = table.checkStatus('gwai306_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                var canDelete = true;
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('crm_gwai306');
                    _temp.GWBZ001 = v.GWBZ001;
                    _temp.GWBZENT = v.GWBZENT;
                    _xdata.push(_temp);
                });
                app.ajax({
                    xurl: 'crm/gwai306/delBatch',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    confirm:{msg: "是否删除选择的数据？"},
                    okmsg: moduleName + '删除成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }else {
                app.showWarning("请先选择数据！");
                return;
            }
        },

        valid: function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwai306_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('crm_gwai306');
                    if (v.GWBZACTI != 'N') {
                        app.showWarning("只有状态为N：无效的信息才可以进行有效操作！");
                        flag = false;
                        return;
                    } else {
                        _temp.GWBZENT = v.GWBZENT;
                        _temp.GWBZ001 = v.GWBZ001;
                        _temp.GWBZACTI = "Y";
                        _xdata.push(_temp);
                    }
                });
            } else {
                app.showWarning("请先选择数据！");
                return;
            }
            if (flag) {
                app.ajax({
                    xurl: 'crm/gwai306/updateStatus',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    confirm:{msg: "是否有效选择的数据？"},
                    okmsg: moduleName + '有效成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }
        },

        invalid: function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwai306_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('crm_gwai306');
                    if (v.GWBZACTI != 'Y') {
                        app.showWarning("只有状态为Y：有效的信息才可以进行无效操作！");
                        flag = false;
                        return;
                    } else {
                        _temp.GWBZENT = v.GWBZENT;
                        _temp.GWBZ001 = v.GWBZ001;
                        _temp.GWBZACTI = "N";
                        _xdata.push(_temp);
                    }
                });
            } else {
                app.showWarning("请先选择数据！");
                return;
            }
            if (flag) {
                app.ajax({
                    xurl: 'crm/gwai306/updateStatus',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    confirm:{msg: "是否无效选择的数据？"},
                    okmsg: moduleName + '无效成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }
        },


        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'gwai306',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            if (field == 'GWBZ001') {
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_gwak001',
                                            columnList: [
                                                {label: '客户编号', prop: 'GWAK001', width: '150'},
                                                {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                                {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWBZ001 = data.GWAK001;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择客户',
                                        area: ['600px', '465px']
                                    }
                                });
                            }
                        }
                    }
                }]
            });
            self.initHeadTable();
        },
        _lazy: function () {
            this._super();
        }
    });

    return new Gwai300();

});